//
//  HLiveToolView.m
//  linktuIOS
//
//  Created by hejunsong on 17/1/11.
//  Copyright © 2017年 tianxie. All rights reserved.
//

#import "HLiveToolView.h"
//#import "DWBubbleMenuButton.h"

#define  anchorSetTag     666
@interface HLiveToolView()

@property(nonatomic,strong) UIButton *playbackplayOrpauseButton;
@property(nonatomic,strong) UIButton *playbackgiftButton;
@property(nonatomic,strong) UIButton *playbackShareButton;
@property(nonatomic,strong) UIButton *playbackfullScreenButton;

@property(nonatomic,strong) NSArray *playbackButtons;

@property(nonatomic,strong) UIButton *anchorsendMessageButton;
@property(nonatomic,strong) UIButton *anchorlinkButton;

@property(nonatomic,strong) UIButton *anchorrequestLikeButton;

@property(nonatomic,strong) UIButton *anchorCloseButton;

@property(nonatomic,strong) UIButton *anchorShareButton;

@property(nonatomic,strong) UIButton *anchorsettingButton;

@property(nonatomic,strong) UIView   *bageView;

@property(nonatomic,strong) NSArray  *anchorButtons;


@property(nonatomic,strong) UIButton *audiencesendMessageButton;
@property(nonatomic,strong) UIButton *audiencelinkButton;
@property(nonatomic,strong) UIButton *audiencegiftButton;
@property(nonatomic,strong) UIButton *audienceShareButton;
@property(nonatomic,strong) UIButton *audiencefullScreenButton;

@property(nonatomic,strong) NSArray *audienceButtons;


@property(nonatomic,strong) UIButton *linksendMessageButton;
@property(nonatomic,strong) UIButton *linklinkButton;
@property(nonatomic,strong) UIButton *linkgiftButton;
@property(nonatomic,strong) UIButton *linkShareButton;
@property(nonatomic,strong) UIButton *linkSetingButton;
@property(nonatomic,strong) NSArray  *linkButtons;

@property(nonatomic,assign) CGFloat buttonWH;

@property(nonatomic,strong) NSArray *playbackplayArray;
@property(nonatomic,strong) NSArray *anchorArray;
@property(nonatomic,strong) NSArray *audienceArray;
@property(nonatomic,strong) NSArray *linkArray;


//@property(nonatomic,strong) DWBubbleMenuButton *anchorsettingupMenuView;
//
//@property(nonatomic,strong) DWBubbleMenuButton *anchorrequestLikeMenuView;

@end

@implementation HLiveToolView

- (instancetype)initWithFrame:(CGRect)frame withStyle:(HLiveToolViewstyle)style
{
    self = [super initWithFrame:frame];
    if (self) {
        
//        _audienceArray = @[@"icon_speak",@"icon_Connect-microphone_Open",@"icon_gift",@"icon_share",@"icon_Full_enlarge"];
        _audienceArray = @[@"icon_speak",@"icon_gift",@"icon_Sign-out"];
        _playbackplayArray = @[@"icon_speak",@"icon_gift",@"icon_Sign-out"];
        _anchorArray   =   @[@"icon_speak",@"icon_Set-up",@"icon_Sign-out"];
    
        _linkArray = @[@"icon_speak",@"icon_Connect-microphone_close",@"icon_gift",@"icon_share",@"icon_Set-up"];
        self.buttonWH = 40;
        switch (style) {
                
                
            case HLiveToolViewstylaudiences: [self initaudienceUI];break;
            case HLiveToolViewstyleanchor: [self initanchorUI];  [self anchorsetingdingdingView]; [self anchorrequestLikedingdingView];  break;
            case HLiveToolViewstyleplayblack: [self initplaybackUI];break;
            case HLiveToolViewstyleLink : [self initLinkUI]; break;
            default: break;
        }
    }
    return self;
}
//审核状态英藏giftbutton
-(void)checkPattern
{
//    if ([[ApplicationSettings instance] isAudit]) {
//        self.audiencegiftButton.hidden = YES;
//        self.playbackgiftButton.hidden = YES;
//        self.linkgiftButton.hidden = YES;
//    }

}
-(void)setStyle:(HLiveToolViewstyle)style
{
    _style = style;
    for(UIView *subview in self.subviews)
    {
        [subview removeFromSuperview];
    }
    switch (style) {
        case HLiveToolViewstylaudiences:{ [self initaudienceUI];}break;
        case HLiveToolViewstyleanchor: [self initanchorUI];  [self anchorsetingdingdingView]; [self anchorrequestLikedingdingView];  break;
        case HLiveToolViewstyleplayblack: [self initplaybackUI];break;
        case HLiveToolViewstyleLink :{ [self  initLinkUI]; [self anchorsetingdingdingView]; }break;
        default: break;
    }

}

-(void)layoutSubviews
{
    [super layoutSubviews];
    NSArray *butsArray = nil;
    switch (_style) {
        case HLiveToolViewstylaudiences:    butsArray = self.audienceButtons; break;
        case HLiveToolViewstyleanchor:      butsArray = self.anchorButtons;break;
        case HLiveToolViewstyleplayblack:   butsArray = self.playbackButtons;break;
        case HLiveToolViewstyleLink :       butsArray = self.linkButtons; break;
        default: break;
    }
    [self layoutButtons:butsArray];
}


-(void)makeplaybackUI
{
    _playbackplayOrpauseButton = [UIButton new];
    _playbackgiftButton = [UIButton new];
//    _playbackShareButton = [UIButton new];
    _playbackfullScreenButton = [UIButton new];
    
    [_playbackplayOrpauseButton setImage:[UIImage imageNamed:@"icon_speak"] forState:UIControlStateSelected];
    
    [self addSubview:_playbackplayOrpauseButton];
    [self addSubview:_playbackgiftButton];
//    [self addSubview:_playbackShareButton];
    [self addSubview:_playbackfullScreenButton];
    
    [_playbackplayOrpauseButton addTarget:self action:@selector(sendmessageClick) forControlEvents:UIControlEventTouchUpInside];
    [_playbackgiftButton addTarget:self action:@selector(gitfClick:) forControlEvents:UIControlEventTouchUpInside];
//     [_playbackShareButton addTarget:self action:@selector(shareClcik) forControlEvents:UIControlEventTouchUpInside];
     [_playbackfullScreenButton addTarget:self action:@selector(fullScreenClcik) forControlEvents:UIControlEventTouchUpInside];
    
    self.playbackButtons = @[_playbackplayOrpauseButton,_playbackgiftButton,_playbackfullScreenButton];
}

-(void)makeLinkUI
{
    _linksendMessageButton = [UIButton new];
    _linklinkButton =        [UIButton new];
    _linkgiftButton =        [UIButton new];
    _linkShareButton =       [UIButton new];
    _linkSetingButton =  [UIButton new];
    
    [_linklinkButton setImage:[UIImage imageNamed:@"icon_Connect-microphone_Open.png"] forState:UIControlStateNormal];
  
    
    [_linksendMessageButton addTarget:self action:@selector(sendmessageClick) forControlEvents:UIControlEventTouchUpInside];
    [_linklinkButton addTarget:self action:@selector(quitlinkClick) forControlEvents:UIControlEventTouchUpInside];
    [_linkgiftButton addTarget:self action:@selector(gitfClick:) forControlEvents:UIControlEventTouchUpInside];
    [_linkShareButton addTarget:self action:@selector(shareClcik) forControlEvents:UIControlEventTouchUpInside];
    [_linkSetingButton addTarget:self action:@selector(anchorShowsetingbuttons:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_linksendMessageButton];
    [self addSubview:_linklinkButton];
    [self addSubview:_linkgiftButton];
    [self addSubview:_linkShareButton];
    [self addSubview:_linkSetingButton];
    self.linkButtons = @[_linksendMessageButton,_linklinkButton,_linkgiftButton,_linkShareButton,_linkSetingButton];


}
-(void)initLinkUI
{
    [self makeLinkUI];
    [self makeFrameWithArray:self.linkButtons images:self.linkArray];
    
}



-(void)makeFrameWithArray:(NSArray *)buttons images:(NSArray *)images
{
    CGFloat leadingpadding = 10;
    CGFloat rightpadding  = 10;
    CGFloat spacepadding = ((WIDTH - leadingpadding - rightpadding) - buttons.count * self.buttonWH) / (buttons.count - 1);
    CGFloat y = 0;
    CGFloat x = 0;
    for (int i = 0; i < buttons.count; i++) {
        x = leadingpadding + (spacepadding + _buttonWH) * i;
        UIButton *button = buttons[i];
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
         button.frame = CGRectMake(x, y, _buttonWH, _buttonWH);
    }
//    [self checkPattern];
}
-(void)layoutButtons:(NSArray *)buttons
{
    CGFloat leadingpadding = 10;
    CGFloat rightpadding  = 10;
    CGFloat spacepadding = ((WIDTH - leadingpadding - rightpadding) - buttons.count * self.buttonWH) / (buttons.count - 1);
    CGFloat y = 0;
    CGFloat x = 0;
    for (int i = 0; i < buttons.count; i++) {
        x = leadingpadding + (spacepadding + _buttonWH) * i;
        UIButton *button = buttons[i];
        button.frame = CGRectMake(x, y, _buttonWH, _buttonWH);
    }


}


-(void)initplaybackUI
{
    [self makeplaybackUI];
    [self makeFrameWithArray:self.playbackButtons images:self.playbackplayArray];

}

-(void)makeanchorUI
{
    _anchorsendMessageButton = [UIButton new];
    _anchorsettingButton = [UIButton new];
    _anchorCloseButton = [UIButton new];
    
    [_anchorlinkButton setImage:[UIImage imageNamed:@"icon_Connect-microphone_Open"] forState:UIControlStateSelected];
    
    [_anchorsendMessageButton addTarget:self action:@selector(sendmessageClick) forControlEvents:UIControlEventTouchUpInside];
    [_anchorsettingButton addTarget:self action:@selector(anchorShowsetingbuttons:) forControlEvents:UIControlEventTouchUpInside];
    [_anchorCloseButton addTarget:self action:@selector(fullScreenClcik) forControlEvents:UIControlEventTouchUpInside];
    
    _bageView = [[UIView alloc] initWithFrame:CGRectMake(_buttonWH - 10, 0, 10, 10)];
    _bageView.layer.cornerRadius = 5;
    _bageView.layer.masksToBounds = YES;
    _bageView.backgroundColor = UIColorFromRGB(0xA6CD36);
    _bageView.hidden = YES;
    [_anchorlinkButton addSubview:_bageView];
    
    
    [self addSubview:_anchorsendMessageButton];
    [self addSubview:_anchorsettingButton];
    [self addSubview:_anchorCloseButton];
    
    self.anchorButtons = @[_anchorsendMessageButton,_anchorsettingButton,_anchorCloseButton];
    

    
}

-(void)initanchorUI
{
    [self makeanchorUI];
    [self makeFrameWithArray:self.anchorButtons images:self.anchorArray];
}


-(void)makeaudienceUI
{
    _audiencesendMessageButton = [UIButton new];
//    _audiencelinkButton =        [UIButton new];
    _audiencegiftButton =        [UIButton new];
//    _audienceShareButton =       [UIButton new];
    _audiencefullScreenButton =  [UIButton new];
    
//     [_audiencelinkButton setImage:[UIImage imageNamed:@"icon_Connect-microphone_Open"] forState:UIControlStateSelected];
    [_audiencegiftButton setImage:[UIImage imageNamed:@"icon_gift"] forState:UIControlStateNormal];
    
    _audiencegiftButton.selected = YES;
    
    _audiencelinkButton.userInteractionEnabled = NO;
    
    [_audiencesendMessageButton addTarget:self action:@selector(sendmessageClick) forControlEvents:UIControlEventTouchUpInside];
//    [_audiencelinkButton addTarget:self action:@selector(requstlinkClick) forControlEvents:UIControlEventTouchUpInside];
    [_audiencegiftButton addTarget:self action:@selector(gitfClick:) forControlEvents:UIControlEventTouchUpInside];
//    [_audienceShareButton addTarget:self action:@selector(shareClcik) forControlEvents:UIControlEventTouchUpInside];
    [_audiencefullScreenButton addTarget:self action:@selector(fullScreenClcik) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_audiencesendMessageButton];
//    [self addSubview:_audiencelinkButton];
    [self addSubview:_audiencegiftButton];
//    [self addSubview:_audienceShareButton];
    [self addSubview:_audiencefullScreenButton];
    self.audienceButtons = @[_audiencesendMessageButton,_audiencegiftButton,_audiencefullScreenButton];
}
-(void)initaudienceUI
{
    [self makeaudienceUI];
    [self makeFrameWithArray:self.audienceButtons images:self.audienceArray];
}
//退出连麦
-(void)quitlinkClick
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolViewCloseLink:)])
    {
        [self.delegate hlivetoolViewCloseLink:self];
    }
    

}

//主播弹出设置
-(void)anchorShowsetingbuttons:(UIButton *)but
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolViewShowAnchorSetView:)])
    {
        [self.delegate hlivetoolViewShowAnchorSetView:self];
    }
    
}
//主播弹出求礼物view
-(void)anchorShowrequstbuttons
{
//    self.anchorrequestLikeMenuView.frame = fromrect;
//    [superView addSubview:self.anchorrequestLikeMenuView];
//    [self.anchorrequestLikeMenuView showButtons];
}
-(void)dismissmenuView
{
//    [self.anchorrequestLikeMenuView dismissButtons];
//    [self.anchorsettingupMenuView dismissButtons];
}

//发送消息
-(void)sendmessageClick
{
    [self dismissmenuView];
    if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolViewsendMessage:)])
    {
        [self.delegate hlivetoolViewsendMessage:self];
    }
    
}
-(void)AnchorOpenOrChecklinkClick
{
    
    [self dismissmenuView];
    if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolViewAnchorLinkClick:)])
    {
        [self.delegate hlivetoolViewAnchorLinkClick:self];
    }

}


//请求连麦
-(void)requstlinkClick
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolViewLink:)])
    {
        [self.delegate hlivetoolViewLink:self];
    }
    
    [self dismissmenuView];
    
}
//分享
-(void)shareClcik
{
    [self dismissmenuView];
    if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolViewShare:)])
    {
        [self.delegate hlivetoolViewShare:self];
    }
}

//礼物
-(void)gitfClick:(UIButton *)but
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolViewSendGift:)])
    {
        [self.delegate hlivetoolViewSendGift:self];
    }

}
-(void)setIsgameOrgift:(BOOL)isgameOrgift
{
    _isgameOrgift = isgameOrgift;
    self.audiencegiftButton.selected = !isgameOrgift;

}

//全屏
-(void)fullScreenClcik
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolViewFullScreen:)])
    {
        [self.delegate hlivetoolViewFullScreen:self];
    }
}
//播放和暂停
-(void)playOrPauseClick:(UIButton *)but
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolView:replayplayorpause:)])
    {
        [self.delegate hlivetoolView:self replayplayorpause:but.selected];
    }
    if(but.selected) {but.selected = NO;}else { but.selected = YES;}
}

//音乐
-(void)musicClcik
{
    [self dismissmenuView];
    if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolViewMusic:)])
    {
        [self.delegate hlivetoolViewMusic:self];
    }
}

-(void)setOpenLink:(BOOL)OpenLink
{
    _OpenLink = OpenLink;
    self.audiencelinkButton.selected = OpenLink;
}

-(void)anchorsetingdingdingView{
    
//        UIImageView *homeLabel = [self createHomeButtonView];
//        _anchorsettingupMenuView = [[DWBubbleMenuButton alloc] initWithFrame:self.anchorsettingButton.frame
//                                                    expansionDirection:DirectionUp];
//        _anchorsettingupMenuView.homeButtonView = homeLabel;
//        homeLabel.center = CGPointMake(_anchorsettingupMenuView.width/2, _anchorsettingupMenuView.height/2);
//        [_anchorsettingupMenuView addButtons:[self   createanchorsetingButtonArray]];
//        _anchorsettingupMenuView.buttonSpacing = 10;
}


-(void)anchorrequestLikedingdingView{
    
//        UIImageView *homeLabel = [self createHomeButtonView];
//        _anchorrequestLikeMenuView = [[DWBubbleMenuButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)                                                                expansionDirection:DirectionUp];
//        _anchorrequestLikeMenuView.homeButtonView = homeLabel;
//        _anchorrequestLikeMenuView.center = CGPointMake(_anchorrequestLikeMenuView.width/2, _anchorrequestLikeMenuView.height/2);
//        [_anchorrequestLikeMenuView addButtons:[self createanchorrequstButtonArray]];
//        _anchorrequestLikeMenuView.buttonSpacing = 10;
   
}


- (UIImageView *)createHomeButtonView {
    
    UIImageView *label = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, 0.f, 36.f, 36.f)];
    label.layer.cornerRadius = label.frame.size.height / 2.f;
    label.clipsToBounds = YES;
    return label;
}

- (NSArray *)createanchorsetingButtonArray {
    NSMutableArray *buttonsMutable = [[NSMutableArray alloc] init];
    int i = 0;
    int tag = 666;
    for (NSString *title in @[@"icon_Flip",@"icon_light",@"icon_Beauty_click"]) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 36, 36)];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:title] forState:UIControlStateNormal];
        button.layer.cornerRadius = button.frame.size.height / 2.f;
        button.backgroundColor = [UIColor clearColor];
        button.clipsToBounds = YES;
        button.tag = tag + i++;
        [button addTarget:self action:@selector(anchorSetClick:) forControlEvents:UIControlEventTouchUpInside];
        [buttonsMutable addObject:button];
    }
    
    return [buttonsMutable copy];
}


- (NSArray *)createanchorrequstButtonArray {
    
    NSMutableArray *buttonsMutable = [[NSMutableArray alloc] init];
    int i = 0; //
    int tag = 734;
    for (NSString *title in @[@"icon_host_gift",@"icon_good",@"icon_follow"]) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 36, 36)];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:title] forState:UIControlStateNormal];
        button.layer.cornerRadius = button.frame.size.height / 2.f;
        button.backgroundColor = [UIColor clearColor];
        button.clipsToBounds = YES;
        button.tag = tag + i++;
        [button addTarget:self action:@selector(anchorrequstClick:) forControlEvents:UIControlEventTouchUpInside];
        [buttonsMutable addObject:button];
    }
    return [buttonsMutable copy];
}


-(void)anchorSetClick:(UIButton *)but
{
    if(but.selected){ but.selected = NO; } else { but.selected = YES; }
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolView:anchorSetType:withOn:)])
        switch (but.tag) {
            case 666:  [self.delegate hlivetoolView:self anchorSetType:HLiveToolAnchorsetTypeFlip   withOn:but.selected]; break;
            case 667:  [self.delegate hlivetoolView:self anchorSetType:HLiveToolAnchorsetTypeLight  withOn:but.selected]; break;
            case 668:  [self.delegate hlivetoolView:self anchorSetType:HLiveToolAnchorsetTypeBeauty withOn:but.selected]; break;
            default: break;
        }
}
-(void)anchorrequstClick:(UIButton *)but
{
   if(self.delegate && [self.delegate respondsToSelector:@selector(hlivetoolView:anchorRequstType:)])
    switch (but.tag) {
        case 734:   [self.delegate hlivetoolView:self anchorRequstType:HLiveToolAnchorRequstTypegift];   break;
        case 735:   [self.delegate hlivetoolView:self anchorRequstType:HLiveToolAnchorRequstTypegood];   break;
        case 736:   [self.delegate hlivetoolView:self anchorRequstType:HLiveToolAnchorRequstTypefollow];   break;
        default: break;
    }

}
-(void)setAllowLinkMc:(BOOL)allowLinkMc
{
    _allowLinkMc = allowLinkMc;
    self.audiencelinkButton.selected = allowLinkMc;
    self.audiencelinkButton.userInteractionEnabled = allowLinkMc;

}
-(void)setAnchorOpenLink:(BOOL)AnchorOpenLink
{
    _AnchorOpenLink = AnchorOpenLink;
    if(_AnchorOpenLink == NO)
    {
        self.anchorlinkButton.selected = NO;
    }else
    {
        self.anchorlinkButton.selected = YES;
    }
}
-(void)setHaveLinkApply:(BOOL)HaveLinkApply
{
    _HaveLinkApply = HaveLinkApply;
    self.bageView.hidden = !_HaveLinkApply;
}

@end
